<template>
  <div class="login-page">
    <!-- 页面头部 -->
    <header class="login-header">
      <div class="header-content">
        <router-link to="/" class="logo">
          <h1>小红书</h1>
        </router-link>
        <div class="header-actions">
          <span>还没有账号？</span>
            <router-link to="/register" class="register-link">
              立即注册
            </router-link>
        </div>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <main class="login-main">
      <div class="login-container">
        <div class="login-card">
          <h2 class="login-title">登录小红书</h2>
          <p class="login-subtitle">发现美好生活</p>

          <!-- 登录表单 -->
          <el-form
            :model="formData"
            :rules="formRules"
            ref="formRef"
            @keyup.enter="handleLogin"
            class="login-form"
          >
            <el-form-item prop="email">
              <el-input
                v-model="formData.email"
                placeholder="请输入邮箱"
                size="large"
                :prefix-icon="Message"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="formData.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                show-password
                :prefix-icon="Lock"
              />
            </el-form-item>
            
            <el-form-item>
              <div class="remember-forgot">
                <el-checkbox v-model="formData.remember">记住我</el-checkbox>
                <router-link to="/forgot-password" class="forgot-link">
                  忘记密码？
                </router-link>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                :loading="loading"
                @click="handleLogin"
                class="login-btn"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 第三方登录 -->
          <div class="divider">
            <span>或</span>
          </div>
          
          <div class="social-login">
            <el-button class="social-btn wechat" :icon="ChatDotRound">
              微信登录
            </el-button>
            <el-button class="social-btn qq" :icon="Iphone">
              QQ登录
            </el-button>
          </div>
        </div>
      </div>
    </main>

    <!-- 页面底部 -->
    <footer class="login-footer">
      <div class="footer-links">
        <a href="#">关于我们</a>
        <a href="#">帮助中心</a>
        <a href="#">隐私政策</a>
        <a href="#">用户协议</a>
      </div>
      <p class="copyright">© 2023 小红书. All rights reserved.</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Message, Lock, ChatDotRound, Iphone } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

const router = useRouter()
const userStore = useUserStore()

// 表单引用和状态
const formRef = ref()
const loading = ref(false)

// 表单数据
const formData = reactive({
  email: '',
  password: '',
  remember: false
})

// 表单验证规则
const formRules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ]
}

// 组件挂载时检查是否有保存的邮箱
onMounted(() => {
  const savedEmail = localStorage.getItem('rememberedEmail')
  if (savedEmail) {
    formData.email = savedEmail
    formData.remember = true
  }
})

// 处理登录
const handleLogin = async () => {
  if (!formRef.value) return
  
  try {
    // 验证表单
    const valid = await formRef.value.validate()
    if (!valid) return
    
    // 记住邮箱
    if (formData.remember) {
      localStorage.setItem('rememberedEmail', formData.email)
    } else {
      localStorage.removeItem('rememberedEmail')
    }
    
    // 开始登录
    loading.value = true
    await userStore.userLogin(formData)
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error) {
    const errorMsg = error.response?.data?.message || '登录失败，请稍后重试'
    ElMessage.error(errorMsg)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8f8f8;
  
  .login-header {
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
    
    .header-content {
      max-width: 1200px;
      margin: 0 auto;
      padding: 0 20px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      
      .logo {
        color: #ff2e4d;
        text-decoration: none;
        
        h1 {
          margin: 0;
          font-size: 24px;
          font-weight: bold;
        }
      }
      
      .header-actions {
        display: flex;
        align-items: center;
        gap: 10px;
        color: #666;
        font-size: 14px;
        
        .register-link {
          color: #ff2e4d;
          font-weight: 500;
          
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
  
  .login-main {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
  }
  
  .login-container {
    width: 100%;
    display: flex;
    justify-content: center;
  }
  
  .login-card {
    background: #fff;
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    width: 400px;
    
    .login-title {
      text-align: center;
      margin-bottom: 10px;
      color: #333;
      font-size: 24px;
      font-weight: bold;
    }
    
    .login-subtitle {
      text-align: center;
      margin-bottom: 30px;
      color: #666;
      font-size: 14px;
    }
    
    .login-form {
      margin-bottom: 20px;
    }
    
    .remember-forgot {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      
      .forgot-link {
        color: #666;
        font-size: 14px;
        
        &:hover {
          color: #ff2e4d;
        }
      }
    }
    
    .login-btn {
      width: 100%;
      margin-top: 10px;
      height: 44px;
      font-size: 16px;
      background-color: #ff2e4d;
      border-color: #ff2e4d;
      
      &:hover {
        background-color: #ff4d64;
        border-color: #ff4d64;
      }
    }
    
    .divider {
      position: relative;
      text-align: center;
      margin: 20px 0;
      
      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 0;
        right: 0;
        height: 1px;
        background: #e8e8e8;
        transform: translateY(-50%);
      }
      
      span {
        position: relative;
        background: #fff;
        padding: 0 15px;
        color: #999;
        font-size: 14px;
      }
    }
    
    .social-login {
      display: flex;
      gap: 15px;
      
      .social-btn {
        flex: 1;
        height: 40px;
        
        &.wechat {
          color: #07C160;
          border-color: #07C160;
          
          &:hover {
            background: rgba(7, 193, 96, 0.1);
          }
        }
        
        &.qq {
          color: #12B7F5;
          border-color: #12B7F5;
          
          &:hover {
            background: rgba(18, 183, 245, 0.1);
          }
        }
      }
    }
  }
  
  .login-footer {
    padding: 30px 0;
    text-align: center;
    background: #fff;
    
    .footer-links {
      margin-bottom: 10px;
      
      a {
        color: #666;
        text-decoration: none;
        margin: 0 10px;
        font-size: 13px;
        
        &:hover {
          color: #ff2e4d;
        }
      }
    }
    
    .copyright {
      color: #999;
      font-size: 12px;
      margin: 0;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-page {
    .login-header {
      .header-content {
        padding: 0 15px;
        
        .logo h1 {
          font-size: 20px;
        }
        
        .header-actions {
          font-size: 13px;
        }
      }
    }
    
    .login-card {
      padding: 30px 20px;
      width: 100%;
      max-width: 350px;
    }
  }
}
</style>